﻿using NUnit.Framework;
using System;
using NHibernate;

namespace NMA.Infrastructure.NHibernate.Tests.Mapping
{
    using NMA.Domain.Model;
    using NMA.Infrastructure.NHibernate.DomainObject;    

    [TestFixture]
    public class PollTest : TestBaseClass
    {
        [Test]
        public void Can_add_poll_to_database()
        {
            var poll = new Poll()
            {
                Id = new Guid(),
                Name = "test poll",
                VoteDate = DateTime.Now,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "abc"
            };            

            //news.AssignPoll(poll);
            //news.AssignCategory(category);
            //news.AssignPoll(poll);
            ISession session = null;

            try
            {
                session = SessionFactory.OpenSession();

                Guid pollId = (Guid)session.Save(poll);
                session.Flush();

                session.Evict(poll);

                var pollDb = session.Get<Poll>(pollId);

                Assert.IsTrue(
                        pollDb.Id == poll.Id && pollDb.Name == poll.Name &&
                        pollDb.VoteDate.ToShortDateString() == poll.VoteDate.ToShortDateString()
                    );
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                session = SessionFactory.OpenSession();                
                session.Delete(poll);
                session.Flush();                
                session.Evict(poll);
            }
        }

        [Test]
        public void Can_update_news_to_database()
        {
            var poll = new Poll()
            {
                Id = new Guid(),
                Name = "test poll",
                VoteDate = DateTime.Now,
                CreatedDate = DateTime.Now,
                UpdatedDate = DateTime.Now,
                CreatedUser = "abc"
            };

            ISession session = null;

            try
            {
                session = SessionFactory.OpenSession();
                Guid id = (Guid)session.Save(poll);
                session.Flush();

                session.Evict(poll);

                var pollDb = session.Get<Poll>(id);

                pollDb.Name = "test update name";
                pollDb.VoteDate = DateTime.Now.AddDays(1);

                session.Update(pollDb);
                session.Flush();

                session.Evict(pollDb);

                var updatePollDb = session.Get<Poll>(pollDb.Id);

                Assert.IsTrue(
                        updatePollDb.Id == pollDb.Id && updatePollDb.Name == pollDb.Name &&
                        updatePollDb.VoteDate.ToShortDateString() == pollDb.VoteDate.ToShortDateString()
                    );
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                session = SessionFactory.OpenSession();                
                session.Delete(poll);
                session.Flush();
                session.Evict(poll);
            }
        }
    }
}
